package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.ContactPeople;
import com.example.zbh.dsliao.ui.adapter.GroupPickContactAdapter;
import com.example.zbh.dsliao.view.FancyIndexer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zbh on 15/12/2.
 */
public class AddGroupMemberActivity extends BaseActivity implements View.OnClickListener, FancyIndexer.OnTouchLetterChangedListener {
    private RelativeLayout queryLayout;
    private TextView add_list_friends;
    private Button add_contact;
    private ListView lv_content;
    private String id;
    private EMGroup group;
    private List<String> member = new ArrayList<>();
    private List<String> list = new ArrayList<>();
    private List<ContactPeople> peoples = new ArrayList<>();
    private GroupPickContactAdapter adapter;
    private FancyIndexer mFancyIndexer;
    private LinearLayout back;

    @Override
    protected int getLayOut() {
        return R.layout.fragment_contactlist;
    }

    @Override
    protected void initView() {
        queryLayout = findeView(R.id.queryLayout);
        add_list_friends = findeView(R.id.add_list_friends);
        add_contact = findeView(R.id.add_contact);
        lv_content = findeView(R.id.lv_content);
        mFancyIndexer = findeView(R.id.bar);
        back = findeView(R.id.back);

    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("userId");
        adapter = new GroupPickContactAdapter();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    group = EMGroupManager.getInstance().getGroupFromServer(id);
                    member = group.getMembers();
                    list = EMContactManager.getInstance().getContactUserNames();

                    for (int i = 0; i < member.size(); i++) {
                        list.remove(member.get(i));
                    }
                    for (int i = 0; i < list.size(); i++) {
                        peoples.add(new ContactPeople(list.get(i)));
                        Log.i("aaaaaaa", peoples.toString());
                    }
                    Collections.sort(peoples);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.addData(peoples);
                            lv_content.setAdapter(adapter);
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        queryLayout.setVisibility(View.GONE);
        add_list_friends.setText("选择联系人");
        add_contact.setOnClickListener(this);
        mFancyIndexer.setOnTouchLetterChangedListener(this);
        back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_contact:
                if (getToBeAddMembers().size() > 0){
                addNewMember();
                }else {
                    Toast.makeText(this, "请选择群成员", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void addNewMember() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    List<String> listM = getToBeAddMembers();
                    String[] members = new String[listM.size()];
                    for (int i = 0; i < listM.size(); i++) {
                        members[i] = listM.get(i);
                    }
                    EMGroupManager.getInstance().addUsersToGroup(id, members);
                    Intent intent = new Intent();
                    intent.putExtra("tianjia","添加");
                    setResult(RESULT_OK, intent);
                    finish();
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private List<String> getToBeAddMembers() {
        return adapter.getAddContactName();
    }

    @Override
    public void onTouchLetterChanged(String s) {
        for (int i = 0; i < peoples.size(); i++) {
            Log.i("nan" , 6 + "" + s);
            ContactPeople people = peoples.get(i);
            String letter = people.getPinyin().charAt(0) + "";
            Log.i("nannannan", letter);
            if(letter.equals(letter.toLowerCase())){
                letter = letter.toUpperCase();
                Log.i("nannannan", letter);
            }
            if (TextUtils.equals(letter, s)){
                Log.i("nan", letter + "----" + s + i);
                lv_content.setSelection(i);
                break;
            }
        }



    }
}
